<template>
	<view>
		<!-- <text>{{visiteId}}</text><br> -->
		<uni-card>
			<text class="card-title">点单详情</text>
			<view class="divider" />
			<view v-for="(item,index) in category" :key="index" style="margin-top: 10px;margin-bottom: 15px;">
				<text
					style="font-size: 18px;opacity:0.8;font-weight: 700;display: block;margin-bottom: 10px;">{{item}}</text>
				<view v-for="(good,key) in informationList" :key="key">
					<view v-if="goodCounts[key] != 0 && good.secondaryClassification == item">
						<text style="margin-left: 15px;font-size: 18px">
							<uni-row style="margin-top: -5%;">
								<uni-col :span="12">
									{{good.entryName}}:
								</uni-col>
								<uni-col :span="6">
									×{{goodCounts[key]}}{{good.unit}}
								</uni-col>
								<uni-col :span="6">
									￥{{goodCounts[key]*good.unitPrice}}
								</uni-col>
							</uni-row>
						</text>
					</view>
				</view>
			</view>
			<view class="divider" />
			<view style="margin-top: 5%;margin-left: 50%;">合计：￥<text
					style="font-size: 22px;font-weight: 700;">{{totalVal}}</text></view>
		</uni-card>
		<button type="" style="width: 40%;display: inline-block;margin-right: 10%;margin-left: 5%;"
			@click="returnBack">返回</button>
		<button type="primary" style="width: 40%;display: inline-block;margin-right: 5%;" @click="confirm">确认</button>
	</view>
</template>

<script>
	import {
		listProject
	} from "@/api/module/wtc/project.js"
	import {
		addOrdering,
		createOrderId
	} from "@/api/module/wtc/ordering.js"
	export default {
		data() {
			return {
				// 订单号
				orderId: '',
				// 游客ID
				visiteId: '',
				// 桌号
				tableId: '',
				// 计数
				goodCounts: [],
				// 总数
				count: 0,
				// 类别
				category: [],
				// 总价
				totalVal: 0,
				// 项目信息表格数据
				informationList: [],
				// 遮罩层
				loading: true,
				// 总条数
				total: 0,
				// 菜品信息查询参数
				queryParams: {
					entryName: null,
					projectCategory: null,
					firstLevelClassification: null,
					secondaryClassification: null,
					tertiaryClassification: null,
					projectCode: null,
					mnemonicCode: null,
					unit: null,
					unitPrice: null,
					commissionPrice: null,
					clockUpTime: null,
					note1: null,
					note2: null,
					note3: null,
					note4: null,
					note5: null,
					note6: null,
					note7: null,
					note8: null,
					note9: null,
					note10: null
				},
				// 点餐信息参数
				queryParamsOrder: {
					visitorId: null,
					tableNumber: null,
					entryName: null,
					projectCategory: null,
					firstLevelClassification: null,
					secondaryClassification: null,
					tertiaryClassification: null,
					projectCode: null,
					mnemonicCode: null,
					unit: null,
					unitPrice: null,
					quantity: null,
					orderNumber: null,
					phoneTime: null,
					totalAmount: null,
					status: null,
					note1: null,
					note2: null,
					note3: null,
					note4: null,
					note5: null,
					note6: null,
					note7: null,
					note8: null,
					note9: null,
					note10: null
				},
			};
		},
		created() {
			// 接参
			const id = this.$route.query.id
			const table = this.$route.query.table;
			const list = this.$route.query.list;
			const c = this.$route.query.count;
			// console.log(list)
			this.visiteId = id
			this.tableId = table
			this.goodCounts = list.split(',')
			for (let i = 0; i < this.goodCounts.length; i++) {
				this.goodCounts[i] = parseInt(this.goodCounts[i])
			}
			this.count = parseInt(c)
			this.getList()
		},
		methods: {
			//返回
			returnBack() {
				// console.log(this.visiteId)
				const vId = this.visiteId
				const tId = this.tableId
				const list = this.goodCounts
				const c = this.count
				this.$tab.navigateTo('/pages/module/wtc/index?id=' + vId + '&table=' + tId + '&list=' + list + '&count=' +
					c);
				// this.$tab.navigateTo('/pages/module/wtc/index?id=' + vId);
			},
			// 查库
			getList() {
				this.loading = true;
				listProject(this.queryParams).then(response => {
					this.informationList = response.rows;
					this.total = response.total;
					this.loading = false;
					// this.goodCounts.length = this.informationList.length
					for (let i = 0; i < this.informationList.length; i++) {
						if (this.goodCounts[i] != 0) {
							if (this.category.includes(this.informationList[i].secondaryClassification)) {
								continue;
							} else {
								this.category.push(this.informationList[i].secondaryClassification)
							}
						}
					}
					for (let i = 0; i < this.informationList.length; i++) {
						this.totalVal += (parseInt(this.informationList[i].unitPrice) * this.goodCounts[i])
					}
				});
			},
			// 提交
			confirm() {
				// 参数设置
				this.queryParamsOrder.visitorId = this.visiteId
				this.queryParamsOrder.tableNumber = this.tableId
				// this.queryParamsOrder.orderNumber = 2 //测试
				createOrderId().then(res => {
					// 订单号生成
					this.queryParamsOrder.orderNumber = res // 通过排序生成，但存在并发问题
					// this.queryParamsOrder.orderNumber = this.visiteId.substring(0,10) + this.tableId // 通过时间戳+桌号生成，但是订单号会很长
					// 循环判断点单
					for (var i = 0; i < this.informationList.length; i++) {
						if (this.goodCounts[i] != 0) {
							this.queryParamsOrder.quantity = this.goodCounts[i]
							this.queryParamsOrder.entryName = this.informationList[i].entryName
							this.queryParamsOrder.projectCategory = this.informationList[i].projectCategory
							this.queryParamsOrder.firstLevelClassification = this.informationList[i]
								.firstLevelClassification
							this.queryParamsOrder.secondaryClassification = this.informationList[i]
								.secondaryClassification
							this.queryParamsOrder.tertiaryClassification = this.informationList[i]
								.tertiaryClassification
							this.queryParamsOrder.projectCode = this.informationList[i].projectCode
							this.queryParamsOrder.mnemonicCode = this.informationList[i].mnemonicCode
							this.queryParamsOrder.unit = this.informationList[i].unit
							this.queryParamsOrder.unitPrice = this.informationList[i].unitPrice
							this.queryParamsOrder.status = 0
							this.queryParamsOrder.totalAmount = (parseFloat(this.informationList[i].unitPrice) *
								this.goodCounts[i])
							this.queryParamsOrder.phoneTime = new Date().getTime()
							addOrdering(this.queryParamsOrder).then(res => {
								const code = this.queryParamsOrder.orderNumber
								const totalVal = this.totalVal
								this.$tab.navigateTo('/pages/module/wtc/code?code=' + code + '&totalVal=' +
									totalVal)
							})
						}
					}
				})
			},
		},

	};
</script>

<style>
	.card-title {
		font-weight: 700;
		font-size: 20px;
	}

	.divider {
		margin-top: 8rpx;
		margin-bottom: 8rpx;
		margin-right: -2%;
		margin-left: -2%;
		background: #E0E3DA;
		width: 104%;
		height: 1rpx;
	}

	.uni-card ::v-deep .uni-card__content {
      padding-left: 5px !important;
      padding-right: 5px !important;
     }

</style>
